<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2022/3/16
 * Time: 3:58 下午
 */

namespace app\api\controller\v33;

use app\api\controller\v13\Auth2;
use app\common\cachemodel\VirtualUserName;
use app\common\cachemodel\ZlcrTag;
use think\Cache;

/**
 * Class GuessQuestion
 * @package app\api\controller\v31
 */
class GuessQuestion extends Auth2
{
    /**
     * 猜你想问详情
     * @return \think\response\Json
     * @throws \Exception
     */
    public function detail()
    {
        $page = input('page/d', 1);
        $id = input('id/d', 0);


        config('article.index');
        $question = (new \app\common\cachemodel\zlcr2\GuessQuestion())->getInfoById($id);

        $questionTypeTips = [
            1 => '本问题仅限星盘解答',
            2 => '本问题仅限骰子解答',
            3 => '本问题仅限塔罗解答',
        ];
        $questionType = (int)($question['question_type'] ?? 0);

        $questionTypeTipItem = $questionTypeTips[$questionType] ?? '';

        $comment = json_decode($question['extend'], true);
        $tags = (new ZlcrTag())->getLevelTag(4, 0);
        $evaluateList = [];
        if ($page === 1)
        {
            foreach ($comment as $item)
            {
                $evaluateList[] = [
                    'nickname' => $item['name'],
                    'avatar' => $item['avatar'],
                    'tag' => $tags[$item['tid']]['tag_name'],
                    'evaluate_content' => $item['content'],
                    'star_level' => $item['score']
                ];
            }
        }
        $userList = $this->getVirtualUser();
        $data = [
            'question_info' => [
                'question_title' => $question['question'],
                'same_question_num' => (string)($question['same_num'] + $question['same_base']),
                'evaluate_num' => (string)($question['comment_num']),
                'user_list' => $userList,
                'tips' => $question['question_des'],
                'tip' => $questionTypeTipItem,
                'target' => 'local',
                'link' => "question_answer_add?qid={$id}&qtype=2&need_auth=2",
            ],
            'evaluate_list' => [
                'total' => count($comment),
                'per_page' => 10,
                'current_page' => 1,
                'last_page' => 1,
                'data' => $evaluateList,
            ],
        ];
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     *  获取虚拟用户
     * @throws \Exception
     */
    protected function getVirtualUser()
    {
        $res = Cache::get('question_virtual_user');
        if ($res === false)
        {
            $index = random_int(0, 1800);
            $data = (new VirtualUserName())->limit($index, 10)->column('name', 'id');
            $res = [];
            foreach ($data as $id => $datum)
            {
                $time = random_int(3, 59);
                $res[] = ['id' => $time, 'str' => "用户 {$datum} {$time}分钟前问了这个问题"];
            }
            array_multisort(array_column($res, 'id'), SORT_NUMERIC, SORT_ASC, $res);// 按 id 排序
            $res = json_encode(array_column($res, 'str'), true);
            Cache::set('question_virtual_user', $res, 3600);
        }
        $res = json_decode($res, true);
        return $res;
    }
}
